package com.pay.ioopos.support.check;

import static com.pay.ioopos.App.DEV_IS_306_308;
import static com.pay.ioopos.App.DEV_IS_FACE;
import static com.pay.ioopos.Constants.INTENT_PARAM_CASE;
import static com.pay.ioopos.Constants.INTENT_PARAM_CODE;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.pay.ioopos.App;
import com.pay.ioopos.KeyCodeFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.support.card.CardFactory;
import com.pay.ioopos.support.scan.ScanCase;
import com.pay.ioopos.support.scan.ScanLife;
import com.pay.ioopos.support.scan.ScanListener;
import com.pay.ioopos.support.scan.s1601.ScanS1601Qrcode;
import com.pay.ioopos.support.scan.sp308.ScanCamera;
import com.pay.ioopos.support.scan.weixin.MyWxPayFace;
import com.pay.ioopos.support.scan.weixin.ScanWxOfflineQrcode;
import com.pay.ioopos.support.scan.weixin.ScanWxOnlineQrcode;
import com.pay.ioopos.support.serialport.Cmd;
import com.pay.ioopos.support.serialport.CmdCase;
import com.pay.ioopos.support.serialport.OnMutipleListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查相机扫码器
 * @author    Moyq5
 * @since  2020/6/16 20:26
 */
public class CheckScanQrcode extends CheckAbstract {
    private ScanLife scanCase;
    private final View.OnKeyListener keyListener = (view, keyCode, event) -> {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            return true;
        }

        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(event.getKeyCode());
        if (null == keyInfo ) {
            return true;
        }
        if (keyInfo == KeyInfo.KEY_NUM_3) {
            warn("检查扫码器：忽略检查");
            stopSpeak("忽略扫码器检查", false);
            return true;
        }
        return false;
    };

    private final ScanListener scanListener = new ScanListener() {

        @Override
        public void onError(String msg) {
            error("检查扫码器：扫码失败->" + msg);
            stopSpeak("扫码失败", false);
        }

        @Override
        public boolean onScan(Intent intent) {
            String barcode = intent.getStringExtra(INTENT_PARAM_CODE);
            info("检查扫码器：扫码成功->" + barcode);
            info("检查扫码器：扫码正常");
            stopSpeak("扫码正常", true);
            return true;
        }
    };

    public CheckScanQrcode(Check... checks) {
        super(checks);
    }

    @Override
    public void onCheck() {

        info("开始检查扫码器>>>>");
        stopSpeak("开始检查扫码器");
        setOnKeyListener(keyListener);

        List<ScanCase> cases = new ArrayList<>();

        // 外接扫码器s1601
        cases.add(new ScanS1601Qrcode(App.getInstance()));

        // 串口扫码
        CmdCase cmdCase = CardFactory.getCmdCase();
        if (null != cmdCase) {
            Cmd cmd = CardFactory.uidOrQrcodeRead(2, new OnMutipleListener() {
                @Override
                public Cmd onSuccess(int flag, String value) {
                    Intent intent = new Intent();
                    intent.putExtra(INTENT_PARAM_CASE, ScanCase.QRCODE);
                    intent.putExtra(INTENT_PARAM_CODE, value);
                    scanListener.onScan(intent);
                    return null;
                }

                @Override
                public Cmd onFail(int flag, String value) {
                    scanListener.onError(value);
                    return null;
                }
            }, cmdCase);
            if (null != cmd) {
                cmdCase.setRootCmd(cmd);
                cases.add(cmdCase);
            }
        }

        // 相机扫码器
        if (DEV_IS_306_308) {
            cases.add(new ScanCamera());
        }

        // 刷脸摄像头扫码
        if (DEV_IS_FACE) {
            if (MyWxPayFace.IS_OFFLINE) {
                cases.add(new ScanWxOfflineQrcode());
            } else {
                cases.add(new ScanWxOnlineQrcode());
            }
        }

        try {
            scanCase = new ScanLife(cases);
            scanCase.setScanListener(scanListener);
            scanCase.bindToLifecycle((LifecycleOwner) getConsole());
        } catch (Throwable e) {
            error("检查扫码器：扫码器异常->" + e.getMessage());
            addSpeak("扫码异常", false);
            return;
        }
        addSpeak("请扫码，忽略请按3");
        info("检查扫码器：请扫描二维码...，忽略请按3");
    }

    @Override
    protected void onTimes(int times) {
        addSpeak("请扫码，忽略请按3");
    }

    @Override
    protected void onTimeout() {
        error("检查扫码器：超时，未能成功扫码");
        stopSpeak("超时，未能成功扫码", false);
    }

    @Override
    protected void release() {
        super.release();
        if (null != scanCase) {
            scanCase.release();
        }
    }

}
